package com.jwdf.uda.ui.activity.presenter;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.bean.UploadImage;
import com.jwdf.uda.event.ReplyDetailEvent;
import com.jwdf.uda.http.AHttpService;
import com.jwdf.uda.http.HttpHelper;
import com.jwdf.uda.http.action.ReplyAddCommentAction;
import com.jwdf.uda.ui.activity.view.IReplyCommentAddView;
import com.jwdf.uda.utils.EventBusUtil;
import com.jwdf.uda.utils.StringUtils;
import com.jwdf.uda.utils.ToastUtils;
import java.util.List;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/10/18.
 */
public class ReplyAddCmtPresenter extends AUploadImagePresenter<IReplyCommentAddView> {
    private int mReplyId;
    private int mAtCommentId;
    private PbBean.PbSignNavType mReType;

    public ReplyAddCmtPresenter(IReplyCommentAddView iView, int replyId, PbBean.PbSignNavType type) {
        super(iView);
        mReplyId = replyId;
        mReType = type;
    }

    public void onHeadActionClick(int commentId) {
        mAtCommentId = commentId;
        String content = mIView.getContent();
        if(content.length() > 140) {
            ToastUtils.show(mIView.getFragment().getContext(),"内容不能多于140个字");
            return;
        }
        List<UploadImage> uploadImages = getUploadImageList();
        if (StringUtils.isNullEmpty(content) && (uploadImages == null || uploadImages.size() <= 0)) {
            ToastUtils.show(getContext(), R.string.guide_comment_add_hint);
            return;
        }
        mIView.showDialogLoading();

        if (uploadImages != null && uploadImages.size() > 0) {
            doUploadImagesAction();
        } else {
            doReplyCommentAddAction(getImageTokenList());
        }
    }

    @Override
    protected List<UploadImage> getUploadImageList() {
        return mIView.getUploadImageList();
    }

    @Override
    protected void doUploadImage(UploadImage uploadImage) {
        mIView.setImageToken(uploadImage);
    }

    @Override
    protected void doUploadImagesSuccess(List<String> imageTokens) {
        doReplyCommentAddAction(imageTokens);
    }

    public void doReplyCommentAddAction(List<String> imageTokens) {
        ReplyAddCommentAction action = ReplyAddCommentAction.newInstance(getContext(), mReplyId, mIView.getContent(), mAtCommentId, imageTokens, mReType);
        mIView.getFragment().onNewRequestCall(action.request(new AHttpService.IResCallback<PbBean.PbResCommentAddComment>() {
            @Override
            public void onCallback(int resultCode, Response<PbBean.PbResCommentAddComment> response, PbBean.PbBaseServerInfo baseServerInfo) {
                if (mIView.getFragment().getView() != null) {
                    mIView.closeDialogLoading();
                    if (HttpHelper.showError(getContext(), resultCode, baseServerInfo, R.string.error_operation)) {
                        return;
                    }
                    doReplyCommentAddSuccess(response.body());
                }
            }
        }));
    }

    private void doReplyCommentAddSuccess(PbBean.PbResCommentAddComment body) {
        mIView.closeCmtDialog();
        EventBusUtil.post(new ReplyDetailEvent(ReplyDetailEvent.EVENT_COMMENT_ADD));
    }
}
